#include "diskmanager.h"
#include "disk.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include "config.h"
#include "util.h"

using std::fstream;
using std::string;

DiskManager::DiskManager()
{

}

Disk* DiskManager::CreateNewDisk(int capacity, string name)
{
    // 文件创建
    ofstream disk_file;
    disk_file.open(name, std::ios::out | std::ios::app);
    if (!disk_file.is_open()) {
        throw "文件打开失败";
    }

    // 地址
//    int address = 0;
    for (int fat_key = 0; fat_key < capacity; fat_key ++) {
//       disk_file<< std::setfill('0') << std::setw(8) ;
//       disk_file << std::setbase(16) << address;
//       address += 16;
           disk_file << "00 ";
    }
//    disk_file << "\n";

//    // 写磁盘区
//    string content = " ";
//    FillBlockSpace(content);
//    for (int block_id = 0; block_id < capacity; block_id ++) {
//        disk_file << content;
//        disk_file << "\n";
//    }

    disk_file.close();

    Disk* new_disk = new Disk(name, capacity);

    new_disk->UpdateFat(0 ,255);

    for (int i = 1; i <= new_disk->GetFatSize() / BLOCK_SIZE; i ++) {
        new_disk->UpdateFat(i, 255);
    }

    return new_disk;
}
